﻿using Commission.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Commission.DAL
{
    public class SchoolContext : DbContext
    {
        public DbSet<Course> Courses { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Instructor> Instructors { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<Person> People { get; set; }
        public DbSet<OfficeAssignment> OfficeAssignments { get; set; }

        public DbSet<Customer> Customers { get; set; }

        public DbSet<CustomerType> CustomerTypes { get; set; }

        public DbSet<PointCommission> PointCommissions { get; set; }

        public DbSet<TransactionHistory> TransactionHistories { get; set; }

        public DbSet<SponsorCommission> SponsorCommissions { get; set; }

        public DbSet<Product> Products { get; set; }

        public DbSet<Unit> Units { get; set; }

        public DbSet<FileUpload> FileUploads { get; set; }

        public DbSet<Trading> Tradings { get; set; }

        public DbSet<UniLevel> UniLevels { get; set; }

        public SchoolContext() : base()
        {
            this.Configuration.LazyLoadingEnabled = true; //Just for test
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            
            modelBuilder.Entity<Customer>()
                .HasOptional(s => s.ParentCustomer)
                .WithMany(s => s.SubCustomers)
                .HasForeignKey(s => s.ParentCustomerID);

            modelBuilder.Entity<Customer>()
                .HasOptional(s => s.SponsorCustomer)
                .WithMany(s => s.SponsoredCustomers)
                .HasForeignKey(s => s.SponsorCustomerID);

            modelBuilder.Entity<SponsorCommission>()
                .HasRequired(d => d.SponsorCustomerType)
                .WithMany(s => s.SponsorCommissions)
                .HasForeignKey(s => s.SponsorCustomerTypeID)
                .WillCascadeOnDelete(false);

            /*
            modelBuilder.Entity<TransactionHistory>()
                .HasOptional(s => s.SponsoredCustomer)
                .WithMany(s => s.TransactionHistories)
                .HasForeignKey(s => s.SponsoredCustomerID);
             */
        }
    }
}